package com.medical.goods.service.impl;

import com.medical.common.to.OrderTo;
import com.medical.goods.dao.CommodityDao;
import com.medical.goods.dao.UserDao;
import com.medical.goods.entity.*;
import com.medical.goods.entity.vo.OrderAuditVo;
import com.medical.goods.entity.vo.OrderDeliverVo;
import com.medical.goods.entity.vo.OrdercheckVo;
import com.medical.goods.entity.vo.OrderVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.medical.common.utils.PageUtils;
import com.medical.common.utils.Query;

import com.medical.goods.dao.OrderDao;
import com.medical.goods.service.OrderService;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderEntity> page = this.page(
                new Query<OrderEntity>().getPage(params),
                new QueryWrapper<OrderEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<OrderVo> orderlist(OrderVo orderVo) {
        return orderDao.orderist(orderVo);
    }


    //订单审核页面
    @Override
    public OrderAuditVo audit(Integer id) {
        OrderEntity orderEntity = orderDao.selectById(id);
        //判断支付状态为已支付  订单状态为申请退款
        if (orderEntity.getPayState().equals("0") && orderEntity.getOrderState().equals("申请退款")){
            return orderDao.audit(id);
        }else{
            System.out.println("请先支付");
        }
        return null;
    }


    //查看订单详情
    @Override
    public OrdercheckVo check(Integer id) {
        return orderDao.check(id);
    }

    //订单发货
    @Override
    public Integer deliver(Integer id,String logisticsName, String logisticsOdd, String logisiticsType, String logisticsPrice, String logisticsRemark)
    {
        OrderEntity orderEntity = orderDao.selectById(id);
        //判断支付状态为已支付 判断发货状态为未发货
        if(orderEntity.getPayState().equals("0") && orderEntity.getDeliverState().equals("1")){
            orderDao.deliver(id,logisticsName,logisticsOdd,logisiticsType,logisticsPrice,logisticsRemark);
            orderEntity.setDeliverState("0");
        }else{
            System.out.println("请先支付");
        }
        return 0;
    }

    @Override
    public void saveSeckillOrder(OrderTo orderTo) {
        OrderEntity orderEntity = new OrderEntity();
        BeanUtils.copyProperties(orderTo,orderEntity);
        orderDao.insert(orderEntity);
        //向延时队列发送信息
        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",orderTo);
//        Integer commodityId = orderTo.getCommodityId();
    }

    @Override
    public void closeOrder(OrderTo orderTo) {
        //查到数据库里对应的订单
        OrderEntity orderEntity = baseMapper.selectOne(new QueryWrapper<OrderEntity>().eq("order_coding", orderTo.getOrderCoding()));
        //关单之前对订单进行判断
        if(orderEntity.getPayState().equals("0")){//未付款状态
            OrderEntity orderUpdate = new OrderEntity();
            orderUpdate.setId(orderEntity.getId());
            orderUpdate.setOrderState("订单已取消");
            baseMapper.updateById(orderUpdate);
        }
    }

    //订单审核提交
    @Override
    public Integer submit(Integer id, Integer dispose, String remark) {
        OrderEntity orderEntity = orderDao.selectById(id);
        if(dispose==1){
            orderEntity.setOrderState("订单已取消");
            orderDao.updateById(orderEntity);
        }else{
            orderEntity.setOrderState("申请退款未通过");
            orderDao.updateById(orderEntity);
        }
        //审核提交
        return  orderDao.submit(id,dispose,remark);

    }


}